app.controller("z-vacation-c", [ '$scope','$rootScope','$z-http','$sce','z-request-s','z-attendance-s','z-restVacation-s','z-edit-menu-s','z-requestApproval-s','z-role-s','$filter',
		function($scope,$rootScope,$zhttp,$sce,Service,AttendanceService,RestVacationService,MenuService,RequestApprovalService,RoleService,$filter) {
		$scope.viewModel = {};
		$scope.viewModel.perPage = "10";
		$scope.viewModel.rowCount = "1";		
		$scope.viewModel.requestTypeList = [];
		$scope.viewModel.roleList=[];
		$scope.viewModel.attendanceStateList=""       //考勤状态
	    $scope.viewModel.approvalTypeList=["0","1","2","3","4","5","6","10","11","12","13"];	
			
		var now = new Date();                     //当前日期     
		var nowMonth = now.getMonth();            //当前月     
		var nowYear=$filter('date')(now,'yyyy');  //当前年
		//下拉多选框长度
		$scope.multiSelectMaxHeight=300;
		//获取当月开始日期
		var monthStartDate = $filter("date")(new Date(nowYear, nowMonth, 1), "yyyy/MM/dd");
		//获取当前日期
        var dayEndDate= $filter("date")(now, "yyyy/MM/dd");
		$scope.viewModel.requestStartDate=new Date(monthStartDate);  //起止日期
		$scope.viewModel.requestEndDate=new Date(dayEndDate);	  	  //终止日期
		//日期控件
		$scope.viewModel.dateOptions = {
				formatYear: 'yy',
			    startingDay: 1,
			    showWeeks:false
		};
		//日期控件
		$scope.viewModel.format = "yyyy/MM/dd";
		//日期启动开关
		$scope.viewModel.startDateOpened = false;
		$scope.viewModel.endDateOpened = false;
		
		//编辑
		$scope.editModel = {};
		$scope.editModel.request={};
		$scope.editModel.request.startToTime=null;
		$scope.editModel.request.hours=null;
		$scope.editModel.dateChangeType=true;
		
		//可用时长
		$scope.editModel.surplusHours=0;
		//可用天数
		$scope.editModel.surplusDayNumber=0;
		$scope.editModel.startToTimeShow = 1;
		
		$scope.editModel.title="";
		$scope.editModel.startDateOpened = false;
		$scope.editModel.endDateOpened = false;
		$scope.editModel.marryDateOpened=false;
		
		
		//查询页面名称
		MenuService.getMenuByUrl({url:'vacation'},
		function(data){	
			$scope.viewModel.menuList=data;
		},function(error) {});
		
		//起止日期选择
		$scope.viewModel.startDateOpen = function($event) {
			$event.preventDefault();
			$event.stopPropagation();
			$scope.viewModel.startDateOpened = !$scope.viewModel.startDateOpened;
		};
		
		//起止日期选择
		$scope.viewModel.endDateOpen = function($event) {
			$event.preventDefault();
			$event.stopPropagation();
			$scope.viewModel.endDateOpened = !$scope.viewModel.endDateOpened;
		};
		
		$scope.editModel.startDateOpen = function($event) {
			$event.preventDefault();
			$event.stopPropagation();
			if($scope.editModel.endDateOpened){
				$scope.editModel.endDateOpened=false;
			}
			if($scope.editModel.marryDateOpened){
				$scope.editModel.marryDateOpened=false;
			}
			$("#vStartTimehsdate").hide();
			$("#vStartToTimehsdate").hide();
			$("#vEndTimehsdate").hide();
			$scope.editModel.startDateOpened = !$scope.editModel.startDateOpened;
		};
		
		$scope.editModel.endDateOpen = function($event) {
			$event.preventDefault();
			$event.stopPropagation();
			if($scope.editModel.startDateOpened){
				$scope.editModel.startDateOpened=false;
			}
			if($scope.editModel.marryDateOpened){
				$scope.editModel.marryDateOpened=false;
			}
			$("#vStartTimehsdate").hide();
			$("#vStartToTimehsdate").hide();
			$("#vEndTimehsdate").hide();
			$scope.editModel.endDateOpened= !$scope.editModel.endDateOpened;
		};
		
		$scope.editModel.marryDateOpen = function($event) {
			$event.preventDefault();
			$event.stopPropagation();
			if($scope.editModel.endDateOpened){
				$scope.editModel.endDateOpened=false;
			}
			if($scope.editModel.startDateOpened){
				$scope.editModel.startDateOpened=false;
			}
			$("#vStartTimehsdate").hide();
			$("#vStartToTimehsdate").hide();
			$("#vEndTimehsdate").hide();
			$scope.editModel.marryDateOpened= !$scope.editModel.marryDateOpened;
		};

		//选择开始时间
		$scope.viewModel.startTimeClick=function(){
			$("#vStartTimehsdate").show();
			$("#vStartToTimehsdate").hide();
			$("#vEndTimehsdate").hide();
			if($scope.editModel.startDateOpened){
				$scope.editModel.startDateOpened=false;
			}
			if($scope.editModel.endDateOpened){
				$scope.editModel.endDateOpened=false;
			}
		};
		
		//选择至
		$scope.viewModel.startToTimeClick=function(){
			$("#vStartTimehsdate").hide();
			$("#vStartToTimehsdate").show();
			$("#vEndTimehsdate").hide();
			if($scope.editModel.marryDateOpened){
				$scope.editModel.marryDateOpened=false;
			}
			if($scope.editModel.startDateOpened){
				$scope.editModel.startDateOpened=false;
			}
			if($scope.editModel.endDateOpened){
				$scope.editModel.endDateOpened=false;
			}
		};
		
		$scope.viewModel.endTimeClick=function(){
			$("#vStartTimehsdate").hide();
			$("#vStartToTimehsdate").hide();
			$("#vEndTimehsdate").show();
			if($scope.editModel.marryDateOpened){
				$scope.editModel.marryDateOpened=false;
			}
			if($scope.editModel.startDateOpened){
				$scope.editModel.startDateOpened=false;
			}
			if($scope.editModel.endDateOpened){
				$scope.editModel.endDateOpened=false;
			}
		};		
		
		//加载考勤状态数据
		$scope.viewModel.bindSelectData = function(){
			AttendanceService.getDictionaryListBydicCls({
				dicCls:"5"
			},
			function(data){
				$scope.viewModel.requestTypeData=data;
				var site = [];  
		        $.each(data, function(index, values) {  
		            site.push({  
		                label : values.dicName,  
		                value : values.dicCd  
		            });  
		        });  
		        $('#vaLattendanceState').multiselect('dataprovider',site);
			},function(error) {
						
			});
			//考勤状态
			$('#vaLattendanceState').multiselect({
				includeSelectAllOption: true,
				enableFiltering: true,
				delimiterText: '; ',
	            maxHeight: $scope.multiSelectMaxHeight
	        });
		};
		$scope.viewModel.bindSelectData();
		
		//加载数据
		$scope.viewModel.bindData = function(){
			var requestTypeList=[];
			if($scope.viewModel.requestTypeList.length<=0){
				requestTypeList=angular.copy($scope.viewModel.approvalTypeList);
			}else{
				requestTypeList=angular.copy($scope.viewModel.requestTypeList);
			}
			$scope.viewModel.isLoading=true;
			//获取基本信息
			Service.getFurloughRequestList({
				pageNo:$scope.viewModel.rowCount,
				pageCount:$scope.viewModel.perPage,
				requestStartDate:$scope.viewModel.requestStartDate,
				requestEndDate:$scope.viewModel.requestEndDate,
				requestTypeList:requestTypeList
			},
			function(data){
				$scope.viewModel.dataList = data.requestBeanList;
				$scope.viewModel.totalData = data.pageTotal;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;
			});
		};
		$scope.viewModel.bindData();
		
		$scope.viewModel.bindRoleData = function(){
			$scope.viewModel.isLoading=true;
			//获取基本信息
			RoleService.getRoleListByEmployeeId({},
			function(data){
				$scope.viewModel.roleList = data;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;
			});
		};
		$scope.viewModel.bindRoleData();
		
		 //格式化验证
        $scope.viewModel.escapeRegExp=function(string){
	        var pattern=/^([0-9]|[0-1][0-9]|2[0-3]):(2[0-9]|[0-5][0-9])$/;
	        var flag = pattern.test(string);
	        return flag;
        };
        
		//加载
		$scope.viewModel.load=function(){
			$scope.viewModel.show=true;
		    //小时控件
			$('#vStartTime').jHsDate({
				format:'hh:mm',
				callBack: function () {
					if($scope.viewModel.escapeRegExp($('#vStartTime').val())){
						$scope.editForm.vStartTime.$valid=true;
						$scope.editForm.vStartTime.$dirty=true;
						$scope.editForm.vStartTime.$invalid=false;
						$scope.editModel.request.startTime=$('#vStartTime').val();
						$scope.editModel.dateChange();
						$scope.editModel.dateChangeType=false;
					}else{
						$('#vEhours').val($scope.viewModel.toHourMinute(0));
						$scope.editModel.request.hours=0;
					}
				}	
			});
			$('#vEndTime').jHsDate({
				format:'hh:mm',
				callBack: function () {
					if($scope.viewModel.escapeRegExp($('#vEndTime').val())){
						$scope.editForm.vEndTime.$valid=true;
						$scope.editForm.vEndTime.$dirty=true;
						$scope.editForm.vEndTime.$invalid=false;
						$scope.editModel.request.endTime=$('#vEndTime').val();
						$scope.editModel.dateChange();
						$scope.editModel.dateChangeType=false;
					}else{
						$('#vEhours').val($scope.viewModel.toHourMinute(0));
						$scope.editModel.request.hours=0;
					}
				}	
			});
			$('#vStartToTime').jHsDate({
				format:'hh:mm',
				callBack: function () {
					if($scope.viewModel.escapeRegExp($('#vStartToTime').val())){
						$scope.editForm.vStartToTime.$valid=true;
						$scope.editForm.vStartToTime.$dirty=true;
						$scope.editForm.vStartToTime.$invalid=false;
						$scope.editModel.request.startToTime=$('#vStartToTime').val();
						$scope.editModel.dateChange();
						$scope.editModel.dateChangeType=false;
					}else{
						$('#vEhours').val($scope.viewModel.toHourMinute(0));
						$scope.editModel.request.hours=0;
					}
				}	
			});
		};
		
		//关闭加载
		$scope.viewModel.unLoad=function(){
			$scope.viewModel.show=false;
			$scope.editModel.request={};
			$scope.editModel.request.startToTime=null;
			$scope.editModel.startToTimeShow = 1;
			$scope.editModel.marchtType=false;
			$scope.editModel.marchtData={};
			$scope.editModel.marchtYear="";
			$scope.editModel.marchtList=[];
		};
		
		//查询验证
		$scope.viewModel.validForm= function(isValid) {
            if (!isValid) {
                return;
            }else{
            	if($scope.viewModel.requestStartDate>$scope.viewModel.requestEndDate){
            		$scope.form.requestEndDate.$invalid=true;
            	}else{
            		$scope.form.requestEndDate.$invalid=false;
            		$scope.viewModel.rowCount=1;
                	$scope.viewModel.bindData();
            	}
            }
        };
		//新增
		$scope.viewModel.add = function(request){
			$scope.editModel.title="新增休假申请";
			$scope.editModel.request.hours=0;
			$scope.editModel.surplusHours=0;
			$scope.editModel.surplusDayNumber=0;
			$scope.editModel.type = true;
			$scope.editModel.request={};
			$scope.editModel.request.reportYear=null;
			$scope.editModel.request.startToTime=null;
			$scope.editModel.request.startDate=null;
			$scope.editModel.request.endDate=null;
			$scope.editModel.request.startTime=null;
			$scope.editModel.request.endTime=null;
			$scope.editModel.hours=null;
			$scope.editModel.request.dayNumber=0;
			$scope.editModel.request.requestType=0;
			$scope.editModel.marchtYear="";
			if(!$scope.editModel.dateChangeType){
				$("#vEhours").val("0");
			}
			if($scope.viewModel.roleList!=null){
				for(var i = 0;$scope.viewModel.roleList.length>i ;i++){
					if($scope.viewModel.roleList[i].isRole==1){
						$scope.editModel.request.roleId=$scope.viewModel.roleList[i].roleId;
					}
				}
			}
			$scope.viewModel.dirty();
			$scope.editModel.selectRequestType();
			$scope.viewModel.load();
		};
		
		
		$scope.viewModel.getRequestHours=function(startToTimeShow){
			if($scope.editModel.request.endDate!=null 
					&& $scope.editModel.request.startDate!=null
					&& $scope.editModel.request.startTime!=null
					&& $scope.editModel.request.endDate!=null
					&& $scope.editModel.request.endTime!=null
					&& $scope.editModel.request.startDate!=null){
						if($scope.editModel.startToTimeShow==0){
							if($scope.editModel.request.startToTime==""
								|| $scope.editModel.request.startToTime==null){
								$scope.editModel.request.hours = 0;
								$scope.editModel.hours=$scope.viewModel.toHourMinute($scope.editModel.request.hours);
								$('#vEhours').val($scope.editModel.hours);
								return;
							}else{
								//判断结束时间是否小于开始时间
								if($scope.editModel.request.startTime>=$scope.editModel.request.startToTime){
									$scope.editModel.request.hours = 0;
									$scope.editModel.hours=$scope.viewModel.toHourMinute($scope.editModel.request.hours);
									$('#vEhours').val($scope.editModel.hours);
									return;
								}
							}
						}
						$scope.editModel.hours=null;
						$('#vEhours').val($scope.editModel.hours);
						Service.getRequestHours($scope.editModel.request,
								function(data){
									$scope.editModel.request.hours = data;
									$scope.editModel.hours=$scope.viewModel.toHourMinute($scope.editModel.request.hours);
									$('#vEhours').val($scope.editModel.hours);
								},function(error) {
									$rootScope.alertError(error.fault.message);
									$scope.editModel.request.hours = 0;
									$scope.editModel.hours=$scope.viewModel.toHourMinute($scope.editModel.request.hours);
									$('#vEhours').val($scope.editModel.hours);
								});
			}else{
				$scope.editModel.request.hours=0;
				$scope.editModel.hours=$scope.viewModel.toHourMinute($scope.editModel.request.hours);
				$('#vEhours').val($scope.editModel.hours);
			}
		};
		
		// 将分钟数量转换为小时和分钟字符串
		$scope.viewModel.toHourMinute=function(minutes){
			if(minutes==null){
				return "";
			}
			return (Math.floor(minutes/60).toFixed(0) + "小时" + Math.floor(minutes%60).toFixed(0) + "分钟" );
		};
		
		//修正
		$scope.viewModel.update=function(request){
			$scope.editModel.title="修改休假申请";
			$scope.editModel.request.hours=0;
			$scope.editModel.surplusHours=0;
			$scope.editModel.surplusDayNumber=0;
			$scope.editModel.type = false;
			$scope.editModel.request=angular.copy(request);
			$scope.editModel.hours=$scope.viewModel.toHourMinute($scope.editModel.request.hours);
			$scope.editModel.request.startDate=new Date(request.startDate);
			$scope.editModel.request.endDate=new Date(request.endDate);
			if(request.endDate!=null){
				$scope.editModel.request.marryDate=new Date(request.marryDate);
			}
			/*if(request.startDate < request.endDate){
				$scope.editModel.startToTimeShow = "0";
			}else{
				$scope.editModel.startToTimeShow != "0";
			}*/
			$scope.editModel.startToTimeShow=1;
			$scope.viewModel.dirty();
			$scope.viewModel.load();
			$scope.editModel.selectRequestType();
		};
		
		$scope.viewModel.dirty=function(){
			//验证不能为空
			$scope.editForm.startDate.$dirty=false;
			$scope.editForm.endDate.$dirty=false;
			$scope.editForm.requestType.$dirty=false;
			$scope.editForm.description.$dirty=false;
			$scope.editForm.vStartTime.$dirty=false;
			$scope.editForm.vEndTime.$dirty=false;
			$scope.editForm.marryDate.$dirty=false;
			$scope.editForm.timeTnterval.$dirty=false;
			//$scope.editForm.vStartToTime.$dirty=false;
		};
		
		//提交
		$scope.viewModel.submit=function(request){
			$scope.editModel.request=angular.copy(request);
			$scope.editModel.request.approvalState=1;
			$scope.editModel.request.startDate=new Date(request.startDate);
			$scope.editModel.request.endDate=new Date(request.endDate);
			$scope.viewModel.isLoading = true;
			if($scope.editModel.request.requestType==3
					|| $scope.editModel.request.requestType==4
					|| $scope.editModel.request.requestType==11
					|| $scope.editModel.request.requestType==13){
				$scope.editModel.request.hours=null;
				$scope.editModel.request.startTime=null;
				$scope.editModel.request.startToTime=null;
				$scope.editModel.request.endTime=null;
			}
			Service.updateRequest($scope.editModel.request,
					function(data){
						$scope.viewModel.unLoad();
						$scope.viewModel.bindData();
						$rootScope.alertInfo("提交成功。");
						$scope.viewModel.isLoading = false;
					},function(error) {
						$rootScope.alertError(error.fault.message);
						$scope.viewModel.isLoading = false;
					});
		};
		
		//验证
		$scope.viewModel.requiredForm= function() {
			if($scope.editModel.request.requestType==12){
				var type=false;
				if(!$scope.editForm.startDate.$valid){
					$scope.editForm.startDate.$dirty=true;
            		type=true;
            	}
				if(!$scope.editForm.description.$valid){
					$scope.editForm.description.$dirty=true;
            		type=true;
            	}
				if(!$scope.editForm.timeTnterval.$valid){
					$scope.editForm.timeTnterval.$dirty=true;
            		type=true;
				}
			}else if($scope.editModel.request.requestType==1
				|| $scope.editModel.request.requestType==0
				|| $scope.editModel.request.requestType==5
				|| $scope.editModel.request.requestType==6
				|| $scope.editModel.request.requestType==10){
				//验证小时类型
				var type=false;
				if(!$scope.editForm.startDate.$valid){
					$scope.editForm.startDate.$dirty=true;
            		type=true;
            	}
				if(!$scope.editForm.endDate.$valid){
					$scope.editForm.endDate.$dirty=true;
            		type=true;
            	}
				if(!$scope.editForm.vStartTime.$valid){
					$scope.editForm.vStartTime.$dirty=true;
            		type=true;
            	}
				if(!$scope.editForm.vEndTime.$valid){
					$scope.editForm.vEndTime.$dirty=true;
            		type=true;
            	}
				if(!$scope.editForm.description.$valid){
					$scope.editForm.description.$dirty=true;
            		type=true;
            	}
				if($scope.editModel.startToTimeShow==0){
					if($scope.editModel.request.startToTime ==null
							|| $scope.editModel.request.startToTime ==""){
						$scope.editForm.vStartToTime.$dirty=true;
						$scope.editForm.vStartToTime.$invalid=true;
						return true;
					}else{
						$scope.editForm.vStartToTime.$invalid=false;
					}
    			}
				//判断日期大小
				if($scope.editModel.request.startDate>$scope.editModel.request.endDate){
					$scope.editForm.endDate.$invalid=false;
					$rootScope.alertWarning("开始日期不能大于结束日期！");
	        		return true;
	        	}else{
	        		if(!type){
	        			$scope.editForm.endDate.$invalid=false;
	        		}
	        	}
				if($scope.editModel.request.startDate < $scope.editModel.request.endDate){
					if($scope.editModel.startToTimeShow==0){
						if($scope.editModel.request.startToTime ==""
							|| $scope.editModel.request.startToTime == null){
							$rootScope.alertWarning("未填写，结束时间，请确认。");
							$scope.editForm.vStartToTime.$dirty=true;
							$scope.editForm.vStartToTime.$invalid=true;
		            		return true;
		            	}
		            	if($scope.editModel.request.startToTime <= $scope.editModel.request.startTime){
		            		$rootScope.alertWarning("结束时间不能小于开始时间。");
							$scope.editForm.vStartToTime.$dirty=true;
							$scope.editForm.vStartToTime.$invalid=true;
		            		return true;
		            	}
		            	$scope.editForm.vStartToTime.$invalid=false;
					}
				}

				if($scope.editModel.request.requestType==1){
					if($scope.editModel.request.hours<60){
						$rootScope.alertWarning("年假低于一小时不允许申请。");
						return true;
					}
					if($scope.editModel.marchtType){
						var yearHours=0;
						for(var mi=0;mi<$scope.editModel.marchtList.length;mi++){
							if($scope.editModel.marchtList[mi].year==$scope.editModel.marchtYear){
								yearHours=$scope.editModel.marchtList[mi].vacation;
							}
						}
						if($scope.editModel.request.hours>yearHours){
							$rootScope.alertWarning("休假时长不能超过可用时长。");
							return true;
						}
					}else{
						if($scope.editModel.request.hours>$scope.editModel.surplusHours){
							$rootScope.alertWarning("休假时长不能超过可用时长。");
							return true;
						}
					}
				}
				else if($scope.editModel.request.requestType==0){
					if($scope.editModel.request.hours<60){
						$rootScope.alertWarning("调休低于一小时不允许申请。");
						return true;
					}
					if($scope.editModel.request.hours>$scope.editModel.surplusHours){
						$rootScope.alertWarning("休假时长不能超过可用时长。");
						return true;
					}
				}else{
					if(!$scope.editModel.marchtType){
						if(($scope.editModel.surplusHours<=0 
								|| $scope.editModel.surplusHours==null)
								&& type==false){
							if($scope.editModel.request.requestType==3){
								if($scope.editModel.totalVacation==0){
								}else{
									$rootScope.alertWarning("没有可用时长，不能保存！");
				            		return true;
								}
							}else if($scope.editModel.request.requestType!=4&&$scope.editModel.request.requestType!=5&&$scope.editModel.request.requestType!=6){
								$rootScope.alertWarning("没有可用时长，不能保存！");
			            		return true;
							}
		            	}
						
						//验证可用时长
		            	if($scope.editModel.request.hours/60 > $scope.editModel.surplusHours){
		            		if($scope.editModel.request.requestType==3){
								if($scope.editModel.totalVacation==0){
								}else{
									$rootScope.alertWarning("不能超过可用时长：" + $scope.editModel.surplusHours + "小时");
				            		return true;
								}
							}else if($scope.editModel.request.requestType!=4&&$scope.editModel.request.requestType!=5&&$scope.editModel.request.requestType!=6){
								$rootScope.alertWarning("不能超过可用时长：" + $scope.editModel.surplusHours + "小时");
			            		return true;
							}
		            	}
		            	$scope.editForm.vStartToTime.$dirty=false;
		            	$scope.editForm.vStartToTime.$invalid=false;
					}
				}
			}else if($scope.editModel.request.requestType==3
					|| $scope.editModel.request.requestType==4
					|| $scope.editModel.request.requestType==11){
				//验证天数类型
				var type=false;
				if(!$scope.editForm.startDate.$valid){
					$scope.editForm.startDate.$dirty=true;
            		type=true;
            	}
				if(!$scope.editForm.endDate.$valid){
					$scope.editForm.endDate.$dirty=true;
            		type=true;
            	}
				if(!$scope.editForm.description.$valid){
					$scope.editForm.description.$dirty=true;
            		type=true;
            	}
				//验证婚假
				if($scope.editModel.request.requestType==3){
					if(!$scope.editForm.marryDate.$valid){
						$scope.editForm.marryDate.$dirty=true;
	            		type=true;
	            	}
					//结婚登记日起一年内生效，日期累加
					var endMarryDate =angular.copy($scope.editModel.request.marryDate);
					endMarryDate.setFullYear(endMarryDate.getFullYear()+1);
					//验证开始日期不能小于结婚登记日
					if($scope.editModel.request.startDate<$scope.editModel.request.marryDate){
						$rootScope.alertWarning("开始日期不能低于结婚登记日！");
	            		return true;
					}
					
					//验证开始日期和结束日期是否在区间内
					if( endMarryDate<$scope.editModel.request.endDate){
						$rootScope.alertWarning("结婚休假截止日："+$filter("date")(endMarryDate, "yyyy/MM/dd"));
	            		return true;
					}
				}
				//判断日期大小
				if($scope.editModel.request.startDate>$scope.editModel.request.endDate){
					$scope.editForm.endDate.$invalid=false;
					$rootScope.alertWarning("开始日期不能大于结束日期！");
	        		return true;
	        	}else{
	        		if(!type){
	        			$scope.editForm.startDate.$invalid=false;
	        			$scope.editForm.endDate.$invalid=false;
	        		}
	        	}
				if($scope.editModel.surplusDayNumber<=0){
					if($scope.editModel.request.requestType==3){
						if(!($scope.editModel.totalVacation==0)){
							$rootScope.alertWarning("没有可用天数，不能保存！");
		            		return true;
						}
					}else if($scope.editModel.request.requestType!=4&&$scope.editModel.request.requestType!=5&&$scope.editModel.request.requestType!=6){
						$rootScope.alertWarning("没有可用天数，不能保存！");
	            		return true;
					}
            	}
				//验证可用时长
            	if($scope.editModel.request.dayNumber> $scope.editModel.surplusDayNumber){
            		if($scope.editModel.request.requestType==3){
						if(!($scope.editModel.totalVacation==0)){
							$rootScope.alertWarning("不能超过可用天数：" + $scope.editModel.surplusDayNumber + "天");
		            		return true;
						}
					}else if($scope.editModel.request.requestType!=4&&$scope.editModel.request.requestType!=5&&$scope.editModel.request.requestType!=6){
						$rootScope.alertWarning("不能超过可用天数：" + $scope.editModel.surplusDayNumber + "天");
	            		return true;
					}
            		
            	}
			}
			return type;
        };
		
        //保存
		$scope.viewModel.saveDate = function(){
			//是否启动可用年假
			if($scope.editModel.marchtType){
				$scope.editModel.request.reportYear=$scope.editModel.marchtYear;
			}else{
				var year=$filter("date")($scope.editModel.request.startDate, "yyyy");
				$scope.editModel.request.reportYear=year;
			}

			if($scope.editModel.request.requestType==3
					|| $scope.editModel.request.requestType==4
					|| $scope.editModel.request.requestType==11
					|| $scope.editModel.request.requestType==13){
				$scope.editModel.request.hours=null;
				$scope.editModel.request.startTime=null;
				$scope.editModel.request.startToTime=null;
				$scope.editModel.request.endTime=null;
			}else if($scope.editModel.request.requestType==12){
				$scope.editModel.request.endDate=$scope.editModel.request.startDate;
				$scope.editModel.request.hours=null;
				$scope.editModel.request.startTime=null;
				$scope.editModel.request.startToTime=null;
				$scope.editModel.request.endTime=null;
			}
			if($scope.editModel.request.requestType==3){
					$rootScope.alertConfirm("婚假申请必须为连续日期，不可分开申请。",function() {
						$scope.viewModel.isLoading = true;
						if($scope.editModel.type){
							Service.saveRequest($scope.editModel.request,
									function(data){
										$scope.viewModel.unLoad();
										$scope.viewModel.bindData();
										$rootScope.alertInfo("新增成功。");
										$scope.viewModel.isLoading = false;
									},function(error) {
										if (angular.isArray(error['fault'])) {
			                                var faultMessage = error['fault'][0]['message'].concat(error['fault'][0]['errorData']);
			                                $rootScope.alertError(faultMessage);
			                            } else {
			                                $rootScope.alertError(error['fault']['message']);
			                            }
										$scope.viewModel.isLoading = false;
									});
						}else{
							Service.updateRequest($scope.editModel.request,
									function(data){
										$scope.viewModel.unLoad();
										$scope.viewModel.bindData();
										$rootScope.alertInfo("修改成功。");
										$scope.viewModel.isLoading = false;
									},function(error) {
										if (angular.isArray(error['fault'])) {
			                                var faultMessage = error['fault'][0]['message'].concat(error['fault'][0]['errorData']);
			                                $rootScope.alertError(faultMessage);
			                            } else {
			                                $rootScope.alertError(error['fault']['message']);
			                            }
										$scope.viewModel.isLoading = false;
									});
						}
					});
			}else{
				$scope.viewModel.isLoading = true;
				if($scope.editModel.type){
					Service.saveRequest($scope.editModel.request,
							function(data){
								$scope.viewModel.unLoad();
								$scope.viewModel.bindData();
								$rootScope.alertInfo("新增成功。");
								$scope.viewModel.isLoading = false;
							},function(error) {
								if (angular.isArray(error['fault'])) {
	                                var faultMessage = error['fault'][0]['message'].concat(error['fault'][0]['errorData']);
	                                $rootScope.alertError(faultMessage);
	                            } else {
	                                $rootScope.alertError(error['fault']['message']);
	                            }
								$scope.viewModel.isLoading = false;
							});
				}else{
					Service.updateRequest($scope.editModel.request,
							function(data){
								$scope.viewModel.unLoad();
								$scope.viewModel.bindData();
								$rootScope.alertInfo("修改成功。");
								$scope.viewModel.isLoading = false;
							},function(error) {
								if (angular.isArray(error['fault'])) {
	                                var faultMessage = error['fault'][0]['message'].concat(error['fault'][0]['errorData']);
	                                $rootScope.alertError(faultMessage);
	                            } else {
	                                $rootScope.alertError(error['fault']['message']);
	                            }
								$scope.viewModel.isLoading = false;
							});
				}
			}
		};
		
		//保存
		$scope.viewModel.save = function(){
			$scope.editModel.request.approvalState=0;
			if($scope.viewModel.requiredForm()){
				return;
			}
			$rootScope.alertConfirm("确定保存吗？",function() {
				$scope.viewModel.saveDate();
			});
		};
		
		//保存并提交
        $scope.viewModel.saveSubmit = function(){
			$scope.editModel.request.approvalState=1;
			if($scope.viewModel.requiredForm()){
				return;
			}
        	$rootScope.alertConfirm("确定保存并提交吗？",function() {
				$scope.viewModel.saveDate();
			});
        };
		
		//删除
		$scope.viewModel.del=function(request){
			$rootScope.alertConfirm("确定删除吗？",function() {
				$scope.viewModel.isLoading=true;
				Service.deleteRequest({
					requestId:request.requestId
				},function(){
					$scope.viewModel.bindData();
					$scope.viewModel.isLoading=false;
					$rootScope.alertInfo("删除成功。");
				},function(error){
					$scope.viewModel.isLoading=false;
					$rootScope.alertError(error.fault.message);
				});
			});
		};
		
		//撤回
		$scope.viewModel.withdraw=function(request){
			$rootScope.alertConfirm("确定撤回吗？",function() {
				$scope.viewModel.isLoading=true;
				Service.getBackRequest({
					requestId:request.requestId
				},function(){
					$scope.viewModel.bindData();
					$scope.viewModel.isLoading=false;
					$rootScope.alertInfo("撤回成功。");
				},function(error){
					$scope.viewModel.isLoading=false;
					$rootScope.alertError(error.fault.message);
				});
			});
		};
		
		//时间框失去焦点
		$scope.editModel.dateChange=function(){
			if($scope.editModel.request.startDate <= $scope.editModel.request.endDate){
				$scope.editModel.request.dayNumber=($scope.editModel.request.endDate.getTime()-$scope.editModel.request.startDate.getTime())/86400000+1;
				$scope.editModel.dayNumberShow = $scope.editModel.request.dayNumber + "天";
			}else{
				$scope.editModel.dayNumberShow = 0;
			}
			if($scope.editModel.request.requestType==1 
					|| $scope.editModel.request.requestType==0 
					|| $scope.editModel.request.requestType==5 
					|| $scope.editModel.request.requestType==6 
					|| $scope.editModel.request.requestType==10){
				if($scope.editModel.request.startDate < $scope.editModel.request.endDate){
					$scope.viewModel.isLoading=true;
					//获取基本信息
					AttendanceService.getAttendanceCount({
						attendanceDate:$scope.editModel.request.startDate,
					},
					function(data){
						$scope.editModel.startToTimeShow = data;
						$scope.viewModel.isLoading=false;
					},function(error) {
						$rootScope.alertError(error.fault.message);
						$scope.viewModel.isLoading=false;
					});
				}else{
					$scope.editModel.startToTimeShow = 1;
					$scope.editModel.request.startToTime = null;
				}
				$scope.viewModel.getRequestHours($scope.editModel.startToTimeShow);
			}
		};
		
		//按钮是否可用
		$scope.viewModel.isDisabled=function(request){
			if(request.approvalState==0){
        		return false;
        	} 
        	return true;
		};
		
		//控制年假显示下拉框
		$scope.editModel.marchtType=false;
		$scope.editModel.marchtData={};
		$scope.editModel.marchtYear="";
		$scope.editModel.marchtList=[];
		
		//类型选择
		$scope.editModel.selected=function(year){
			if($scope.editModel.marchtYear==year){
				return true;
			}
			return false;
		};
		
		//类型选择
		$scope.editModel.selectRequestType = function(){
			if($scope.editModel.request.requestType!=12){
				//判断年假，并判断开始日期，是否有1,2月份
				if($scope.editModel.request.requestType==1){
					if($scope.editModel.request.startDate!=null){
						var year=$filter("date")($scope.editModel.request.startDate, "yyyy");
						var march=$filter("date")($scope.editModel.request.startDate, "MM");
						$scope.viewModel.isLoading=true;	
						//验证该时间是否可用去年年假
						Service.getCheckYearFurlough({
								yearStr:year,
								monthStr:march
							},
							function(data){
								 if(data){
									 $scope.editModel.getLastYearYearFurlough();
								 }else{
									 $scope.editModel.marchtType=false;
									 $scope.editModel.getRestVacationByType();
									 $scope.viewModel.isLoading=false;
								 }		
							},function(error) {
								$rootScope.alertError(error.fault.message);
								$scope.viewModel.isLoading=false;	
							});
					}
				}else{
					$scope.editModel.marchtType=false;
					$scope.editModel.getRestVacationByType();
				}
			}
		};
		
		//查询去年年假和今年年假
		$scope.editModel.getLastYearYearFurlough=function(){
			$scope.viewModel.isLoading=true;
			$scope.editModel.marchtList=[];
			Service.getLastYearYearFurlough({
				startDate :$scope.editModel.request.startDate,
				},
				function(data){
					$scope.editModel.marchtData=data;
					//判断是否为修改年假
					if($scope.editModel.request.reportYear!=null){
						$scope.editModel.marchtYear=$scope.editModel.request.reportYear+"";
					}else{
						$scope.editModel.marchtYear="";
					}
					if($scope.editModel.marchtData.lastYear!=null
							&& $scope.editModel.marchtData.lastYearRestVacation!=0){
						if($scope.editModel.marchtYear==""){
							$scope.editModel.marchtYear=$scope.editModel.marchtData.lastYear;
						}
						var lateDate={};
						lateDate.year=$scope.editModel.marchtData.lastYear;
						lateDate.vacation=$scope.editModel.marchtData.lastYearRestVacation;
						$scope.editModel.marchtList.push(lateDate);
					}
					if($scope.editModel.marchtData.thisYear!=null
							&& $scope.editModel.marchtData.restVacation!=0){
						if($scope.editModel.marchtYear==""){
							$scope.editModel.marchtYear=$scope.editModel.marchtData.thisYear;
						}
						var thisDate={};
						thisDate.year=$scope.editModel.marchtData.thisYear;
						thisDate.vacation=$scope.editModel.marchtData.restVacation;
						$scope.editModel.marchtList.push(thisDate);
					}
					//如果为空
					if($scope.editModel.marchtList.length<=0){
						$scope.editModel.marchtYear=$scope.editModel.marchtData.lastYear;
						var lateDate={};
						lateDate.year=$scope.editModel.marchtData.lastYear;
						lateDate.vacation=0;
						$scope.editModel.marchtList.push(lateDate);
						var thisDate={};
						thisDate.year=$scope.editModel.marchtData.thisYear;
						thisDate.vacation=0;
						$scope.editModel.marchtList.push(thisDate);
					}
					$scope.editModel.marchtType=true;
					$scope.viewModel.isLoading=false;									
				},function(error) {
					$rootScope.alertError(error.fault.message);
					$scope.viewModel.isLoading=false;
				});
		};
		
		
		$scope.editModel.getRestVacationByType=function(){
			//获取基本信息
			RestVacationService.getRestVacationByType({
				furloughType : $scope.editModel.request.requestType,
				startDate :$scope.editModel.request.startDate,
				},
			function(data){
				if($scope.editModel.request.requestType==0
						|| $scope.editModel.request.requestType==5
						|| $scope.editModel.request.requestType==6
						|| $scope.editModel.request.requestType==10){
					
					$scope.editModel.surplusHours=data.restVacation; //data
					if($scope.editModel.surplusHours!=null){
						//$scope.editModel.surplusHoursAndMin = Math.floor(a/60)+"小时"+ Math.floor(a%60)+"分钟";
						$scope.editModel.surplusHoursAndMin =$scope.viewModel.toHourMinute($scope.editModel.surplusHours);
					}
					//$scope.editModel.surplusHoursShow = 
					$scope.editModel.totalVacation=data.totalVacation;
				}else if($scope.editModel.request.requestType==3
						|| $scope.editModel.request.requestType==4
						|| $scope.editModel.request.requestType==11){
					$scope.editModel.surplusDayNumber=data.restVacation; //data
					$scope.editModel.surplusDayNumberShow=data.restVacation + "天";
					$scope.editModel.totalVacation=data.totalVacation;
				}else if($scope.editModel.request.requestType==1){
					$scope.editModel.surplusHours=data.restVacation; //data
					if($scope.editModel.surplusHours!=null){
						$scope.editModel.surplusHoursAndMin =$scope.viewModel.toHourMinute($scope.editModel.surplusHours);
					}
				}else if($scope.editModel.request.requestType==13){
					$scope.editModel.surplusDayNumber=data.restVacation;
					$scope.editModel.surplusDayNumberShow=data.restVacation + "天";
				}
				$scope.viewModel.isLoading=false;
			},function(error) {
				$rootScope.alertError(error.fault.message);
				$scope.viewModel.isLoading=false;
			});
		};
		
		
		/*  状态详情 */
		$scope.viewApprovalRecordModel = {};
		// 点击列表申请状态
		$scope.viewModel.selecttApprovalRecord = function(request){
			$scope.viewApprovalRecordModel.show = true;
			$scope.viewApprovalRecordModel.isLoading=true;
			$scope.viewApprovalRecordModel.title = "查看审批过程";
			//获取基本信息
			RequestApprovalService.getApprovalRecordInfoById({
				requestId:request.requestId,
			},
			function(data){
				$scope.viewApprovalRecordModel.dataList = data;
				$scope.viewApprovalRecordModel.isLoading=false;
			},function(error) {
				$rootScope.alertError(error.fault.message);
				$scope.viewApprovalRecordModel.isLoading=false;
			});
		}
		
		//关闭加载
		$scope.viewApprovalRecordModel.unLoad=function(){
			$scope.viewApprovalRecordModel.show=false;
		};
		

} ]);